package com.FH.organization.work_flow_history;

import com.FH.organization.manage_line.ManageLineResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/12/11 0011.
 */
@Stateless
public class WorkFlowHistoryDAOImpl extends AccessDB implements WorkFlowHistoryDAO {

    @Inject
    ManageLineResource manageLineResource;

    /**
     * 流程审批记录查询
     *
     * @param search
     * @param page
     * @param size
     */
    @Override
    public ResultDTO getWorkFlowHistoryInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from work_flow_history h where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("work_flow_history_id")) {
                sql_w += " AND h.work_flow_history_id ='" + json.getString("work_flow_history_id") + "'";
            }

            if (json.has("work_flow_id")) {
                sql_w += " AND h.work_flow_id = '" + json.getString("work_flow_id") + "'";
            }

            if (json.has("work_flow_formid")) {
                sql_w += " AND h.work_flow_formid = '" + json.getString("work_flow_formid") + "'";
            }
            if (json.has("work_flow_approval_organization_id")) {
                sql_w += " AND h.work_flow_approval_organization_id = '" + json.getString("work_flow_approval_organization_id") + "'";
            }
            if (json.has("work_flow_approval")) {
                sql_w += " AND h.work_flow_approval = '" + json.getString("work_flow_approval") + "'";
            }
            if (json.has("work_flow_type")) {
                sql_w += " AND h.work_flow_type = '" + json.getString("work_flow_type") + "'";
            }
            if (json.has("work_flow_status")) {
                sql_w += " AND h.work_flow_status = '" + json.getString("work_flow_status") + "'";
            }
            if (json.has("source_key")) {
                sql_w += " AND h.source_key = '" + json.getString("source_key") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<WorkFlowHistoryShowDTO> result = new ArrayList<WorkFlowHistoryShowDTO>();
            if (!existName.equals("0")) {
                String sql = "SELECT h.work_flow_history_id,h.work_flow_id,h.work_flow_formid,h.work_flow_approval_organization_id," +
                        "h.work_flow_approval,h.work_flow_suggestion,h.work_flow_type,h.work_flow_date,h.work_flow_finish_date,h.work_flow_status,m.member_name,o.organization_name,h.source_key" +
                        " FROM work_flow_history h LEFT OUTER JOIN kam_member m ON h.work_flow_approval = m.member_id " +
                        "LEFT OUTER JOIN organization o ON m.organization_id = o.organization_id WHERE 1=1 " +
                        sql_w + " ORDER BY h.work_flow_finish_date " + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    WorkFlowHistoryShowDTO workFlowHistoryShowDTO = new WorkFlowHistoryShowDTO();
                    workFlowHistoryShowDTO.setWork_flow_history_id(seresult.get(i)[1]);
                    workFlowHistoryShowDTO.setWork_flow_id(seresult.get(i)[2]);
                    workFlowHistoryShowDTO.setWork_flow_formid(seresult.get(i)[3]);
                    workFlowHistoryShowDTO.setWork_flow_approval_organization_id(seresult.get(i)[4]);
                    workFlowHistoryShowDTO.setWork_flow_approval(seresult.get(i)[5]);
                    workFlowHistoryShowDTO.setWork_flow_suggestion(seresult.get(i)[6]!=null?seresult.get(i)[6]:"");
                    workFlowHistoryShowDTO.setWork_flow_type(seresult.get(i)[7]);
                    workFlowHistoryShowDTO.setWork_flow_date(seresult.get(i)[8]);
                    workFlowHistoryShowDTO.setWork_flow_finish_date(seresult.get(i)[9]);
                    workFlowHistoryShowDTO.setWork_flow_finish_day(seresult.get(i)[9].split(" ")[0]);//只显示年月日
                    workFlowHistoryShowDTO.setWork_flow_status(seresult.get(i)[10]);
                    workFlowHistoryShowDTO.setMember_name(seresult.get(i)[11]);
                    workFlowHistoryShowDTO.setOrganization_name(seresult.get(i)[12]);
                    workFlowHistoryShowDTO.setSource_key(seresult.get(i)[13]);

                    //获取下一节点信息
                    if(null!=seresult.get(i)[3]&&!"".equals(seresult.get(i)[3])&&null!=seresult.get(i)[5]&&!"".equals(seresult.get(i)[5])&&null!=seresult.get(i)[7]&&!"".equals(seresult.get(i)[7])){

                        String sql_target = "";
                        if(json.has("manage_id")){
                            if(null!=seresult.get(i)[13]&&!"".equals(seresult.get(i)[13])){
                                sql_target = "SELECT DISTINCT(k.member_id),k.member_name,o.organization_id,o.organization_name FROM kam_member k " +
                                        "JOIN organization o ON k.organization_id = o.organization_id JOIN manageline l ON k.member_id = l.targetId " +
                                        "JOIN work_flow_history h ON l.sourceId = h.work_flow_approval AND l.source_key = h.source_key " +
                                        "WHERE h.work_flow_formid='"+seresult.get(i)[3]+"' AND h.work_flow_approval='"+seresult.get(i)[5]+"' " +
                                        "AND l.manage_id = '"+json.getString("manage_id")+"' AND h.source_key = '" +seresult.get(i)[13]+"'";
                            }else {
                                sql_target = "SELECT DISTINCT(k.member_id),k.member_name,o.organization_id,o.organization_name FROM kam_member k " +
                                        "JOIN organization o ON k.organization_id = o.organization_id " +
                                        "JOIN manageline l ON k.member_id = l.targetId " +
                                        "JOIN work_flow_history h ON l.sourceId = h.work_flow_approval " +
                                        "WHERE h.work_flow_formid='"+seresult.get(i)[3]+"' AND h.work_flow_approval='"+seresult.get(i)[5]+"' AND l.manage_id = '"+json.getString("manage_id")+"'";
                            }
                        }else {
                            if(null!=seresult.get(i)[13]&&!"".equals(seresult.get(i)[13])){
                                sql_target = "SELECT DISTINCT(k.member_id),k.member_name,o.organization_id,o.organization_name FROM kam_member k " +
                                        "JOIN organization o ON k.organization_id = o.organization_id JOIN manageline l ON k.member_id = l.targetId " +
                                        "JOIN work_flow_history h ON l.sourceId = h.work_flow_approval AND l.source_key = h.source_key " +
                                        "JOIN bpmanage m ON h.work_flow_type = m.table_name AND l.manage_id = m.manage_id " +
                                        "WHERE h.work_flow_formid='"+seresult.get(i)[3]+"' AND h.work_flow_approval='"+seresult.get(i)[5]+"' " +
                                        "AND h.work_flow_type = '"+seresult.get(i)[7]+"' AND h.source_key = '" +seresult.get(i)[13]+"'";
                            }else {
                                sql_target = "SELECT DISTINCT(k.member_id),k.member_name,o.organization_id,o.organization_name FROM kam_member k " +
                                        "JOIN organization o ON k.organization_id = o.organization_id " +
                                        "JOIN manageline l ON k.member_id = l.targetId " +
                                        "JOIN work_flow_history h ON l.sourceId = h.work_flow_approval " +
                                        "JOIN bpmanage m ON h.work_flow_type = m.table_name AND l.manage_id = m.manage_id " +
                                        "WHERE h.work_flow_formid='"+seresult.get(i)[3]+"' AND h.work_flow_approval='"+seresult.get(i)[5]+"' AND h.work_flow_type = '"+seresult.get(i)[7]+"'";
                            }
                        }

                        List<String[]> result_target = getReportingTo(sql_target);

                        List<TargetMemberShowDTO> targetMemberList = new ArrayList<TargetMemberShowDTO>();

                        for(int j=0;j<result_target.size();j++){
                            TargetMemberShowDTO targetMemberShowDTO = new TargetMemberShowDTO();
                            targetMemberShowDTO.setMember_id(result_target.get(j)[1]);
                            targetMemberShowDTO.setMember_name(result_target.get(j)[2]);
                            targetMemberShowDTO.setOrganization_id(result_target.get(j)[3]);
                            targetMemberShowDTO.setOrganization_name(result_target.get(j)[4]);

                            targetMemberList.add(targetMemberShowDTO);
                        }

                        workFlowHistoryShowDTO.setTargetMemberList(targetMemberList);
                    }
                    result.add(workFlowHistoryShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
                resultDTO.setValues(result);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    /**
     * 流程审批记录添加
     *
     * @param dataDTO
     */
    @Override
    public ResultDTO addWorkFlowHistoryInfo(WorkFlowHistoryPullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();//构建返回对象

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String insert = "INSERT INTO work_flow_history (work_flow_history_id,work_flow_id,work_flow_formid,work_flow_approval_organization_id," +
                "work_flow_approval,work_flow_suggestion,work_flow_type,work_flow_date,work_flow_finish_date,work_flow_status,source_key) VALUES ('"+uuid+"','"+dataDTO.getWork_flow_id()+"','"+
                dataDTO.getWork_flow_formid()+"','"+dataDTO.getWork_flow_approval_organization_id()+"','"+dataDTO.getWork_flow_approval()+"','"+
                dataDTO.getWork_flow_suggestion()+"','"+dataDTO.getWork_flow_type()+"','"+dataDTO.getWork_flow_date()+"','"+dataDTO.getWork_flow_finish_date()+"','"+dataDTO.getWork_flow_status()+"','"+(dataDTO.getSource_key()!=null?dataDTO.getSource_key():"")+"')";

        int result = this.addReportingTo(insert);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 流程审批记录更新
     *
     * @param dataDTO
     */
    @Override
    public ResultDTO updateWorkFlowHistoryInfo(WorkFlowHistoryPullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = simpleDateFormat.format(new Date());

        String update = "UPDATE work_flow_history SET work_flow_finish_date = '"+time+"'";

        if(null!=dataDTO.getWork_flow_suggestion()&&!"".equals(dataDTO.getWork_flow_suggestion())){
            update += ",work_flow_suggestion = '"+dataDTO.getWork_flow_suggestion()+"'";
        }
        if(null!=dataDTO.getWork_flow_status()&&!"".equals(dataDTO.getWork_flow_status())){
            update += ",work_flow_status = '"+dataDTO.getWork_flow_status()+"'";
        }

        String sql_w = "";

        if(null!=dataDTO.getWork_flow_id()&&!"".equals(dataDTO.getWork_flow_id())){
            sql_w += " AND work_flow_id = '"+dataDTO.getWork_flow_id()+"'";
        }
        if(null!=dataDTO.getWork_flow_formid()&&!"".equals(dataDTO.getWork_flow_formid())){
            sql_w += " AND work_flow_formid = '"+dataDTO.getWork_flow_formid()+"'";
        }
        if(null!=dataDTO.getWork_flow_approval_organization_id()&&!"".equals(dataDTO.getWork_flow_approval_organization_id())){
            sql_w += " AND work_flow_approval_organization_id = '"+dataDTO.getWork_flow_approval_organization_id()+"'";
        }
        if(null!=dataDTO.getWork_flow_approval()&&!"".equals(dataDTO.getWork_flow_approval())){
            sql_w += " AND work_flow_approval = '"+dataDTO.getWork_flow_approval()+"'";
        }
        if(null!=dataDTO.getWork_flow_type()&&!"".equals(dataDTO.getWork_flow_type())){
            sql_w += " AND work_flow_type = '"+dataDTO.getWork_flow_type()+"'";
        }
        if(null!=dataDTO.getWork_flow_date()&&!"".equals(dataDTO.getWork_flow_date())){
            sql_w += " AND work_flow_date = '"+dataDTO.getWork_flow_date().substring(0,dataDTO.getWork_flow_date().length()-2)+"'";
        }

        update += " WHERE 1=1" + sql_w;

        int result = this.addReportingTo(update);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 流程审批记录删除
     *
     * @param dataDTO
     */
    @Override
    public ResultDTO deleteWorkFlowHistoryInfo(WorkFlowHistoryPullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();

        //判断是否存在
        String sql = "select count(1) from work_flow_history where work_flow_formid = '"+dataDTO.getWork_flow_formid()+"'";
        String exist = getReportingToOne(sql);

        int result = 0;
        if(!exist.equals("0")){
            String delete = "delete from work_flow_history where work_flow_formid = '"+dataDTO.getWork_flow_formid()+"'";
            result = addReportingTo(delete);
            System.out.println(delete);
        }
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
